/*
 * @Descripttion: 
 * @Author: voanit
 * @Date: 2021-07-29 14:53:54
 * @LastEditors: voanit
 * @LastEditTime: 2021-08-05 17:03:03
 */
'use strict';
// import jsEncrypt from 'jsencrypt'; // RSA 非对称 加密
import jsEncrypt from '@/node_modules_update/jsencrypt.js'; // RSA 非对称 加密
// import cryptoJs from 'crypto-js'; // AES 对称 加密
class Encrypt {
  constructor() {
    // 公钥
    this.pubKey = `-----BEGIN PUBLIC KEY-----
    MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDdA8aYe+rQ9rRnc0TL/Q+5Dv7D
    1+UTfp7ok0OgWwC7KwE2jmuxRQV/62EuZvzO5hZnXB87Jm5zBNeEmGsKk4FzN3Ex
    dlmn53X13WDsPcwFsXUMdw0QYD//MJoxxfZGyRVHeiIcuAhoMj1pM1+wDGreNVDY
    puySFTv8woEbgzd0hQIDAQAB
    -----END PUBLIC KEY-----`;
    // key 基础字段
    this.rootStr = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
    this.rndNum = 0;
  };

  // RSA 非对称加密
  keyJsencrypt (str) {
    // 非对称加密
    const crypt = new jsEncrypt();
    // 设置公钥
    crypt.setPublicKey(this.pubKey);
    const enc = crypt.encrypt(encodeURIComponent(str));
    return enc;
  };

  // RSA 非对称解密
  privDecrypt (str) {
    const crypt = new JSEncrypt()
    crypt.setPrivateKey(privKey)
    return decodeURIComponent(crypt.decrypt(str))
  }

  // AES 加密
  encrypt (word, key) {
    const aeskey = CryptoJs.enc.Utf8.parse(key);
    const srcs = CryptoJs.enc.Utf8.parse(word);
    const encrypted = CryptoJs.AES.encrypt(srcs, aeskey, { mode: CryptoJs.mode.ECB });
    return encrypted.toString();
  };
  // AES 解密
  decrypt (word, key) {
    const aeskey = CryptoJs.enc.Utf8.parse(key);
    const decrypt = CryptoJs.AES.decrypt(word, aeskey, { mode: CryptoJs.mode.ECB });
    return CryptoJs.enc.Utf8.stringify(decrypt).toString();
  }

  hashSHA246 (params) {
    return JSON.stringify(params).toString()
  }

}
export default Encrypt;
